﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain.Values
{
	using System;
	using System.Web.Mvc;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Core.Validators;

	public class UserUpdate : ValueBase<UserUpdate>
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="UserUpdate"/> class.
		/// </summary>
		public UserUpdate()
		{

		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UserUpdate"/> class.
		/// </summary>
		/// <param name="user">The user.</param>
		public UserUpdate(User user) : this()
		{
			this.Id = user.Id;
			this.Name = user.Name;
			this.CurrentName = user.Name;
			this.EmailAddress = user.EmailAddress;
			this.CurrentEmailAddress = user.EmailAddress;
			this.VerifyEmailAddress = user.EmailAddress;
			this.Status = user.Status;
		}

		/// <summary>
		/// Gets or sets the id.
		/// </summary>
		/// <value>The id.</value>
		public int Id
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the name.
		/// </summary>
		/// <value>The name.</value>
		public string Name
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the current name.
		/// </summary>
		/// <value>The current name.</value>
		public string CurrentName
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the email address.
		/// </summary>
		/// <value>The email address.</value>
		public string EmailAddress
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the current email address.
		/// </summary>
		/// <value>The current email address.</value>
		public string CurrentEmailAddress
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the verify email address.
		/// </summary>
		/// <value>The verify email address.</value>
		public string VerifyEmailAddress
		{
			get;
			set;
		}

		/// <summary>
		/// Gets a value indicating whether [name changed].
		/// </summary>
		/// <value><c>true</c> if [name changed]; otherwise, <c>false</c>.</value>
		public bool NameChanged
		{
			get
			{
				return !this.Name.Equals(this.CurrentName);
			}
		}

		/// <summary>
		/// Gets a value indicating whether [email address changed].
		/// </summary>
		/// <value><c>true</c> if [email address changed]; otherwise, <c>false</c>.</value>
		public bool EmailAddressChanged
		{
			get
			{
				return !this.EmailAddress.Equals(this.CurrentEmailAddress);
			}
		}

		/// <summary>
		/// Gets or sets the status.
		/// </summary>
		/// <value>The status.</value>
		public UserStatus Status
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the statuses.
		/// </summary>
		/// <value>The statuses.</value>
		public SelectList Statuses
		{
			get
			{
				return new SelectList(Enum.GetNames(typeof(UserStatus)), this.Status.Name());
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<UserUpdate> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Name)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Name));

			if (this.NameChanged)
			{
				rules.Add(x => x.Name)
					.Custom(new UserUniqueNameValidator(), Resources.Errors.UserNameTaken);
			}

			rules.Add(x => x.EmailAddress)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.EmailAddress));

			if (this.EmailAddressChanged)
			{
				rules.Add(x => x.EmailAddress)
					.Custom(new UserUniqueEmailAddressValidator(), Resources.Errors.UserEmailAddressTaken);
			}

			rules.Add(x => x.VerifyEmailAddress)
				.Equal(x => x.EmailAddress, Resources.Errors.Equal.Fmt(Resources.Titles.EmailAddress, Resources.Titles.VerifyEmailAddress));

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}